package ycl.common.utils;

import com.alibaba.fastjson.JSONObject;
import ycl.common.constants.BaseConstant;
import ycl.common.models.res.IdCardResolve;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 用户工具
 *
 * @author ycl
 * @date 2022/6/26 0026 0:30:52
 */
public class UserUtil {


	/**
	 * 根据用户身份证解析用户信息
	 *
	 * @param idCard
	 */
	public static IdCardResolve getUserInfoByIdCard(String idCard) {
		boolean b = RegularUtil.idCard(idCard);
		if (!b)
			throw new IllegalArgumentException("身份证校验不通过");
		IdCardResolve resolve = new IdCardResolve();
		String area = idCard.substring(0, 6);
		String year = idCard.substring(6, 10);
		String month = idCard.substring(10, 12);
		String day = idCard.substring(12, 14);
		int sex = Integer.parseInt(idCard.substring(16, 17));

		String bir = year + "-" + month + "-" + day;
		System.out.println(bir);
		LocalDate birthday = LocalDate.parse(bir, DateTimeFormatter.ofPattern(BaseConstant.DATE_PATTERN));

		resolve.setArea(area)
				.setYear(Integer.parseInt(year))
				.setMonth(Integer.parseInt(month))
				.setDay(Integer.parseInt(day))
				.setBirthday(birthday)
				.setSex(sex % 2 != 0);
		return resolve;
	}

	public static void main(String[] args) {
		System.out.println(JSONObject.toJSONString(getUserInfoByIdCard("440882199912067218")));
	}


}
